/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAEventCell.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 10/18/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAEventCell.h"
#import "BALibrarySettings.h"
#import "LazyLoadManager.h"
#import "InfoGalleriImageUrlUtils.h"

@interface BAEventCell()

@property (weak, nonatomic) IBOutlet UIView *highlightView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingIndicator;

@property (weak, nonatomic) IBOutlet UIView *primaryTitleContainer;
@property (weak, nonatomic) IBOutlet UIView *secondaryTitleContainer;
@property (weak, nonatomic) IBOutlet UILabel *secondaryTitleLabel;

@property (weak, nonatomic) IBOutlet UILabel *monthLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;


@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dateViewTopSpaceConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dateViewBottmSpaceConstraint;


@end

@implementation BAEventCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)prepareForReuse{
    [self.coverImageView setImage:nil];
}

-(void)awakeFromNib{
    if (RD_IS_IPAD) {
        BA_LABEL_SET_FONT_REGULAR(self.titleLabel);
        BA_LABEL_SET_FONT_LIGHT(self.secondaryTitleLabel);
    } else {
        BA_LABEL_SET_FONT_LIGHT(self.titleLabel);
    }
    BA_LABEL_SET_FONT_LIGHT(self.dayLabel);
    BA_LABEL_SET_FONT_MEDIUM(self.monthLabel);
    BA_LABEL_SET_FONT_LIGHT(self.dateLabel);
    
    if (self.arrowImageView != nil) {
        UIImage* arrowImage = [[BALibrarySettings instance] getCachedImageNamed:@"white_arrow_right" multiplyWithColor:[BALibrarySettings instance].getCustomerColorNormal];
        [self.arrowImageView setImage:arrowImage];
    }
}

-(void)layoutSubviews{
    if (self.frame.size.height > 200) {
        [self.secondaryTitleContainer setHidden:NO];
        [self.primaryTitleContainer setHidden:YES];
        self.secondaryTitleLabel.textColor = [[BALibrarySettings instance] getCustomerColorDark];
        
        //move date label up to top left (with insets), by switching priority of y-deciding constraints
        self.dateViewBottmSpaceConstraint.priority = UILayoutPriorityDefaultLow;
        self.dateViewTopSpaceConstraint.priority = UILayoutPriorityDefaultHigh;
        
    } else{
        [self.secondaryTitleContainer setHidden:YES];
        [self.primaryTitleContainer setHidden:NO];
    }
    [super layoutSubviews];
}

-(void)setHighlighted:(BOOL)highlighted{
    [self setHighlighted:highlighted animated:NO];
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [self.highlightView setHidden:!highlighted];
    //[super setHighlighted:highlighted animated:animated];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)dealloc{
    [[LazyLoadManager instance] cancelConnectionsForOwner:self];
}

-(void)setEvent:(BAEvent *)event{
    if (_event != event) {
        _event = event;
        
        [self.titleLabel setText:event.headline];
        [self.secondaryTitleLabel setText:[event.headline uppercaseString]];
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:event.startTime];
        
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"]];
        if (self.monthLabel != nil) {
            [dateFormatter setDateFormat:@"MMM"];
            [self.monthLabel setText:[[dateFormatter stringFromDate:date] uppercaseString]];
        }
        if (self.dayLabel != nil) {
            [dateFormatter setDateFormat:@"d"];
            [self.dayLabel setText:[dateFormatter stringFromDate:date]];
        }
        
        if (self.dateLabel != nil) {
            [dateFormatter setDateFormat:@"d. MMMM"];
            // Create date string from formatter, using the current date
            NSString* dateString = [dateFormatter stringFromDate:date];
            [dateFormatter setDateFormat:@"HH:mm"];
            // Create date string from formatter, using the current date
            NSString* timeString = [dateFormatter stringFromDate:date];
            
            [self.dateLabel setText:[NSString stringWithFormat:@"%@ kl. %@",dateString,timeString]];
        }
        
        
        
        if (event.imageUrl.length > 0) {
            NSString* resize_url;
            if (RD_IS_IPAD) {
                //We download images larger than needed, as the cells may vary in size!
                resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:event.imageUrl toWidth:400/*256*/ toHeight:400 usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_BOUNDING_BOX outputFormat:@"jpeg" doubleUpForRetina:YES];
            } else{
                CGSize imageSize = self.coverImageView.frame.size;
                resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:event.imageUrl toWidth:imageSize.width toHeight:imageSize.height usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_CROP_EDGES outputFormat:@"jpeg" doubleUpForRetina:YES];
            }
            
            [[LazyLoadManager instance] lazyLoadUIImageFromUrl:resize_url withOwner:self setScaleforRetina:YES onSuccess:^(UIImage *image) {
                [self.coverImageView setImage:image];
                [UIView animateWithDuration:0.25 animations:^{
                    [self.coverImageView setAlpha:1];
                } completion:^(BOOL finished) {
                    [self.loadingIndicator stopAnimating];
                }];
            } onError:^(LazyLoaderErrorCode errorCode, NSString *errorMessage) {
                
            }];
        } else{
            [self.loadingIndicator stopAnimating];
        }
    }
}


@end
